package com.portal.admin.userAuthor.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.portal.admin.base.entity.BaseService;
import com.portal.admin.userAuthor.dao.UserAuthorDao;
import com.portal.admin.userAuthor.entity.UserAuthor;
import com.portal.admin.utils.admin.ObjectUtils;
import com.portal.admin.utils.admin.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//授权信息接口类
@Service
public class UserAuthorService extends BaseService {

    @Autowired
    private UserAuthorDao userAuthorDao;

    //【授权信息】设置查询条件
    private LambdaQueryWrapper<UserAuthor> getUserAuthorQueryCondition(UserAuthor userAuthor) {
        LambdaQueryWrapper<UserAuthor> lambdaQuery = this.getBaseQueryCondition(userAuthor);
        //根据创建时间排序
        lambdaQuery.orderByDesc(UserAuthor::getCreateDate);
        if (ObjectUtils.isNotNull(userAuthor)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(userAuthor.getId()), UserAuthor::getId, userAuthor.getId());
            //【昵称】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(userAuthor.getNickName()), UserAuthor::getNickName, userAuthor.getNickName());
            //【性别】精确查询
            lambdaQuery.eq(StringUtils.isNotNull(userAuthor.getGender()), UserAuthor::getGender, userAuthor.getGender());
            //【国家】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(userAuthor.getCountry()), UserAuthor::getCountry, userAuthor.getCountry());
            //【省份】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(userAuthor.getProvince()), UserAuthor::getProvince, userAuthor.getProvince());
            //【城市】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(userAuthor.getCity()), UserAuthor::getCity, userAuthor.getCity());
            //【用户标识】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(userAuthor.getOpenid()), UserAuthor::getOpenid, userAuthor.getOpenid());
            //【唯一标识】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(userAuthor.getUnionid()), UserAuthor::getUnionid, userAuthor.getUnionid());
        }
        return lambdaQuery;
    }

    //【授权信息】分页查询
    public IPage<UserAuthor> getPage(Page<UserAuthor> page, UserAuthor userAuthor) {
        LambdaQueryWrapper<UserAuthor> lambdaQuery = getUserAuthorQueryCondition(userAuthor);
        return userAuthorDao.selectPage(page, lambdaQuery);
    }
    
    //【授权信息】查询列表
    public List<UserAuthor> getList(UserAuthor userAuthor) {
        LambdaQueryWrapper<UserAuthor> lambdaQuery = getUserAuthorQueryCondition(userAuthor);
        return userAuthorDao.selectList(lambdaQuery);
    }
    
    //【授权信息】根据id查询
    public UserAuthor get(String id) {
        return userAuthorDao.selectById(id);
    }

    //【授权信息】根据对象查询
    public UserAuthor get(UserAuthor userAuthor) {
    LambdaQueryWrapper<UserAuthor> lambdaQuery = getUserAuthorQueryCondition(userAuthor);
        return userAuthorDao.selectOne(lambdaQuery);
    }
    
    //【授权信息】新增
    public int insert(UserAuthor userAuthor) {
        this.preInsert(userAuthor);
        return userAuthorDao.insert(userAuthor);
    }
    
    //【授权信息】修改
    public int update(UserAuthor userAuthor) {
        this.preUpdate(userAuthor);
        return userAuthorDao.updateById(userAuthor);
    }
    
    //【授权信息】删除
    public int delete(String id) {
        return userAuthorDao.deleteById(id);
    }

}